var padding_map = {
	top: 20,
	rigth: 70,
	bottom: 0,
	left: 20
};

var signsItems = {
	Chinese_name: ["球队", "赛季", "投篮", "命中", "出手", "三分", "三分命中", "三分出手", "罚球", "罚球命中", "罚球出手",
		"助攻", "篮板", "前场板", "后场板", "盖帽", "犯规", "抢断", "失误", "得分", "胜场数", "败场数", "失分"
	],
	English_name: ["Team", "Season", "Shoot", "S_Hit", "S_Shot", "Threes", "T_Hit", "T_Shot", "Penalty", "P_Hit",
		"P_Shot",
		"Assists", "Backboard", "Forecourt", "Backcourt", "Block", "Foul", "Steals", "Turnover", "Score",
		"Win", "Lose", "Lose_points"
	]
};

var Season = ["00-01", "01-02", "02-03", "03-04", "04-05", "05-06", "06-07", "07-08", "08-09",
	"09-10", "10-11", "11-12", "12-13", "13-14", "14-15", "15-16", "16-17", "17-18", "18-19"
];

var Team = ["亚特兰大老鹰", "布鲁克林篮网", "波士顿凯尔特人", "夏洛特黄蜂", "芝加哥公牛", "克里夫兰骑士", "达拉斯独行侠", "丹佛掘金",
	"底特律活塞", "金州勇士", "休斯顿火箭", "印第安纳步行者", "洛杉矶快船", "洛杉矶湖人", "孟菲斯灰熊", "迈阿密热火", "密尔沃基雄鹿", "明尼苏达森林狼",
	"新奥尔良鹈鹕", "纽约尼克斯", "俄克拉荷马雷霆", "奥兰多魔术", "费城76人", "菲尼克斯太阳", "波特兰开拓者", "萨克拉门托国王",
	"圣安东尼奥马刺", "多伦多猛龙", "犹他爵士", "华盛顿奇才"
];

function getSeason() {
	return Season;
}

function getTeams() {
	var Teams = [];
	d3.csv("data/NBA-teams.csv", function(d) {
		for (var i = 0; i < Team.length; ++i) {
			var t = {};
			for (var j = 0; j < d.length; ++j) {
				if (d[j].ChineseName == Team[i]) {
					t.Team = Team[i];
					t.abb = d[j].abb;
					t.EASTorWEST = d[j].EASTorWEST;
					t.NO = i;
					Teams[i] = t;
					break;
				}
			}
		}
	}); //csv
	return Teams;
}

function getSigns() {
	return signsItems;
}

var div = d3.select(".map_select");

var div1 = document.getElementsByClassName("map_select")[0];

//获取div的宽高
var width = div1.offsetWidth - padding_map.left - padding_map.rigth;
var height = div1.offsetHeight - padding_map.top - padding_map.bottom;

var other = d3.select(".map_select")
	.append("div")
	.attr("id", "other");

function getSelect() {
	return _select;
}

var svg = div.append("svg")
	.attr("class", "map")
	.attr("preserveAspectRatio", "xMidYMid meet")
	.attr("viewBox", "0 0 " + width + " " + height);

var projection = d3.geo.albersUsa()
	.translate([width / 5 * 2, height / 5 * 2])
	.scale([width * 1.5]);

var path = d3.geo.path()
	.projection(projection);

var zoom = d3.behavior.zoom()
	.scaleExtent([1, 10]) //用于设置最小和最大的缩放比例
	.on("zoom", zoomed)

var map_g = svg.append("g")
	.attr("class", "map_g")
	.call(zoom)
	.attr("transform", "translate(" + padding_map.left + "," + padding_map.top + ")");

d3.csv("data/US-states.csv", function(error, csvData) {
	d3.json("data/US-geo.json", function(jsonData) {
		//判斷城市屬於東部或者西部
		for (i = 0; i < csvData.length; i++) {
			var dataState = csvData[i].state; //Grab state name
			var dataValue = parseFloat(csvData[i].value); //Grab data value, and convert from string to float
			var dataEASTorWEST = csvData[i].EASTorWEST;
			//Find the corresponding state inside the GeoJSON
			for (var j = 0; j < jsonData.features.length; j++) {
				var jsonState = jsonData.features[j].properties.name;
				if (dataState == jsonState) {
					//Copy the data value into the JSON
					jsonData.features[j].properties.EASTorWEST = dataEASTorWEST;
					//Stop looking through the JSON
					break;
				}
			}
		}
		//繪製地圖
		map_g.selectAll("path")
			.data(jsonData.features)
			.enter()
			.append("path")
			.attr("d", path)
			.attr("stroke", "white")
			.attr("stroke-width", 2)
			.attr("class", function(d) {
				return d.properties.postal;
			})
			.attr("transform", "translate(" + padding_map.left + "," + padding_map.top + ")")
			.style("fill", function(d) {
				var EastorWest = d.properties.EASTorWEST;
				if (EastorWest) {
					if (EastorWest == "East")
						return "#C6E2FF";
					else return "yellow";
				} else return "#ccc";
			});
		d3.csv("data/NBA-teams.csv", function(data) {
			var index_map = 0;
			DrawParallel("00-01");
			DrawAllRose("00-01");
			DrawBar('洛杉矶湖人', '00-01');
			Rose_ClickTeam('洛杉矶湖人', '00-01');
			map_g.selectAll("image")
				.data(data)
				.enter()
				.append("image")
				.attr("class", function(d) {
					return d.abb;
				})
				.attr("xlink:href", function(d) {
					return "logo/" + d.abb + "_logo.svg";
				})
				.attr("width", 40 + "px")
				.attr("height", 40 + "px")
				.attr("x", function(d) {
					if (d.abb == 'PHI') {
						return projection([d.lon, d.lat])[0] + 30;
					} else if (d.abb == 'NYK') {
						return projection([d.lon, d.lat])[0] + 20 ;
					} else return projection([d.lon, d.lat])[0];
				})
				.attr("y", function(d) {
					if (d.abb == 'PHI') {
						return projection([d.lon, d.lat])[1] + 20;
					} else if (d.abb == 'NYK') {
						return projection([d.lon, d.lat])[1] ;
					} else return projection([d.lon, d.lat])[1];
				})
				.attr("transform", "translate(" + padding_map.left + "," + padding_map.top + ")")
				.on("click", ClickTeam)
				.on("mouseover", MouseOver)
				.on("mouseout", MouseOut);
		})
	});
});

function ClickTeam(d) {
	var _TeamName = d.ChineseName;
	var _Season;
	var index = select_season.selectedIndex;
	_Season = select_season.options[index].text.split("赛季")[0];
	selectTeam(_TeamName, _Season);
	DrawBar(_TeamName, _Season);
	Rose_ClickTeam(_TeamName,_Season);
}

function MouseOver(d) {
	if (d.EASTorWEST == "West") {
		d3.select(this)
			.transition()
			.attr("width", 80 + "px")
			.attr("height", 60 + "px")
			.duration(200);
		d3.select(this).append("svg:title")
			.text(function(d) {
				console.log(d);
				return d.ChineseName + '('+d.abb + ')';
			});
	} else {
		d3.select(this)
			.transition()
			.attr("width", 80 + "px")
			.attr("height", 60 + "px")
			.attr("transform", "translate(" + (padding_map.left - 40) + "," + (padding_map.top) + ")")
			.duration(200);
		d3.select(this).append("svg:title")
			.text(function(d) {
				return d.ChineseName + '('+d.abb + ')';
			});	
	}

}

function MouseOut(d) {
	if (d.EASTorWEST == "West") {
		d3.select(this)
			.transition()
			.attr("width", 40 + "px")
			.attr("height", 40 + "px")
			.duration(200);
	} else {
		d3.select(this)
			.transition()
			.attr("width", 30 + "px")
			.attr("height", 30 + "px")
			.attr("transform", "translate(" + (padding_map.left) + "," + (padding_map.top) + ")")
			.duration(200);
	}
}

function zoomed() {
	map_g.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")")
}

